const getAlias = () => {
  const paths = require('../paths');
  const path = require('path');
  const fs = require('fs');

  const cpaasConfig = paths.getCpaasBootConfig(paths.appPath) || {};
  let externalAlias = {};
  try {
    let aliasConfigFile =
      cpaasConfig.aliasConfigFile || `${paths.appConfig}/alias`;
    if (typeof cpaasConfig.alias === 'string') {
      aliasConfigFile = cpaasConfig.alias;
    }
    try {
      aliasConfigFile = require.resolve(
        path.resolve(paths.appRootPath, aliasConfigFile)
      );
    } catch (e) {
      aliasConfigFile = null;
    }
    if (aliasConfigFile) {
      externalAlias = {
        ...require(aliasConfigFile),
      };
    }
    if (typeof cpaasConfig.alias === 'object') {
      externalAlias = {
        ...externalAlias,
        ...cpaasConfig.alias,
      };
    }
  } catch (e) {
    // externalAlias = {
    //   components: 'cpaas-front/lib/components/',
    //   utils: 'cpaas-front/lib/utils/',
    //   services: 'cpaas-front/lib/services/',
    //   ...externalAlias,
    // };
    console.error(e);
    process.exit();
  }
  const aliasConfig = {
    // '@': paths.appSrc,
    '@src': paths.appSrc,
    '@config': paths.appConfig,
    'cpaas-front/lib/index': path.resolve(__dirname, '../mock/emptyfile.js'),
    // './getModuleRouters': path.resolve(
    //   __dirname,
    //   '../mock/getModuleRouters.js'
    // ),
    // Support React Native Web
    // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
    'react-native': 'react-native-web',
    ...externalAlias,
  };

  // if (
  //   externalAlias['@common'] &&
  //   !externalAlias['cpaas-boot-customize-init-config']
  // ) {
  //   aliasConfig['cpaas-boot-customize-init-config'] = `${externalAlias[
  //     '@common'
  //   ] || path.resolve(paths.appRootPath, './src')}/config/customize`;
  // }

  if (!aliasConfig['cpaas-boot-customize-init-config']) {
    aliasConfig['cpaas-boot-customize-init-config'] = path.resolve(
      __dirname,
      '../mock/emptyfile.js'
    );
  }

  if (!aliasConfig['@global-config']) {
    const globalConfigFile = [
      'src/app.js',
      'src/app.ts',
      'src/app.tsx',
    ].find((filePath) =>
      fs.existsSync(path.resolve(paths.appRootPath, filePath))
    );
    if (globalConfigFile) {
      aliasConfig['@global-config'] =
        path.resolve(paths.appRootPath, globalConfigFile) ||
        path.resolve(__dirname, '../mock/emptyfile.js');
    } else {
      aliasConfig['@global-config'] = path.resolve(
        __dirname,
        '../mock/emptyfile.js'
      );
    }
  }

  if (paths.pwdIsRoot) {
    let rootRouterFile = path.resolve(paths.appRootPath, 'src/utils/router.js');
    if (!fs.existsSync(rootRouterFile)) {
      rootRouterFile = path.resolve(paths.appRootPath, 'src/utils/router.tsx');
      if (!fs.existsSync(rootRouterFile)) {
        rootRouterFile = path.resolve(paths.appRootPath, 'src/utils/router.ts');
      }
    }
    if (fs.existsSync(rootRouterFile)) {
      aliasConfig['@root-src-utils-router'] = rootRouterFile;
    } else {
      aliasConfig['@root-src-utils-router'] =
        'cpaas-boot/lib/entry/root/emptyRouter';
    }
  }
  if (!aliasConfig['@']) {
    aliasConfig['@'] = paths.appSrc;
  }
  return aliasConfig;
};

module.exports = getAlias;
